﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jhong.FunMVC.Razor
{
    using System.ComponentModel;
    using Westwind.RazorHosting;

    public partial class RazorHTMLHelper
    {
        private RazorHTMLHelper() { }

        public static RazorHTMLHelper HTML { get { return new RazorHTMLHelper(); } }

        public static RawString Raw(string html)
        {
            return new RawString(html);
        }

        internal IDictionary<string, object> AnonymousObjectToHtmlAttributes(object htmlAttributes)
        {
            if (null == htmlAttributes) return new Dictionary<string, object>();
            var properties = TypeDescriptor.GetProperties(htmlAttributes);
            var dic = new Dictionary<string, object>(properties.Count);
            foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(htmlAttributes)) dic.Add(propertyDescriptor.Name.Replace('_', '-'), propertyDescriptor.GetValue(htmlAttributes));
            return dic;
        }

        internal string AnonymousObjectToHtmlString(object htmlAttributes)
        {
            var attributes = this.AnonymousObjectToHtmlAttributes(htmlAttributes);
            var htmlStringSb = new StringBuilder();
            foreach (var attibute in attributes)
            {
                htmlStringSb.Append(string.Format("{0}='{1}' ", attibute.Key, attibute.Value));
            }
            return htmlStringSb.ToString();
        }



    }
}
